package com.enn.demo.core.service.impl.domain.model.person;

import com.enn.demo.core.service.impl.domain.model.ValueObject;
import com.enn.demo.core.service.impl.util.DateUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * @author kll
 * @date 2021/5/17
 */
public final class Birthday implements ValueObject<Birthday> {

    private Date birthday;

    public Birthday(String birthday){
        if(StringUtils.isBlank(birthday)) {
            throw new IllegalArgumentException("用户birthday不能为空");
        }
        try {
            birthday = StringUtils.substring(birthday,0,10);
            this.birthday = DateUtils.stringToDate(birthday, DateUtils.DATE_PATTERN);
        } catch (Exception e){
            this.birthday = new Date();
        }
    }

    public Birthday(Date birthday){
        this.birthday = birthday;
    }

    public Date getBirthday() {
        return birthday;
    }

    @Override
    public boolean sameValueAs(Birthday other) {
        return other != null && this.birthday.compareTo(other.getBirthday())==0;
    }
}
